package engine;

import serv.*;

public class Parser {
	
	private int mvt = 3;
	
	public Parser () {}
	
	public void readMsg (String msg, World w, int nbt, ThreadManager tm){
		if (msg.charAt(0) ==  ('1')) {
			// Check if the player doesn't escape the world 
			if ((w.getPlayers()[nbt].getPosY() - 1) >= 0){
				// We change the position of the player
				w.getPlayers()[nbt].setPosY(w.getPlayers()[nbt].getPosY() - mvt);
			}
		}
		if (msg.charAt(1) ==  ('1')) {
			// Check if the player doesn't escape the world 
			if ((w.getPlayers()[nbt].getPosY() + 1) <= w.getSWorldY()){
				// We change the position of the player
				w.getPlayers()[nbt].setPosY(w.getPlayers()[nbt].getPosY() + mvt);
			}
		}
		if (msg.charAt(2) ==  ('1')) {
			// Check if the player doesn't escape the world 
			if ((w.getPlayers()[nbt].getPosX() + 1) <= w.getSWorldX()){
				// We change the position of the player
				w.getPlayers()[nbt].setPosX(w.getPlayers()[nbt].getPosX() + mvt);
			}
		}
		if (msg.charAt(3) ==  ('1')) {
			if ((w.getPlayers()[nbt].getPosX() - 1) >= 0){
				// We change the position of the player
				// We change the position of the player
				w.getPlayers()[nbt].setPosX(w.getPlayers()[nbt].getPosX() - mvt);
			}
		}
		if (msg.charAt(4) ==  ('1')) {
			// We change the position of the player
			if (w.getPlayers()[nbt].getAngle() == 0){
				w.getPlayers()[nbt].setAngle(359);
			}
			else {
				w.getPlayers()[nbt].setAngle(w.getPlayers()[nbt].getAngle() - mvt);	
			}
		}
		if (msg.charAt(5) ==  ('1')) {
			// We change the position of the player
			if (w.getPlayers()[nbt].getAngle() == 359){
				w.getPlayers()[nbt].setAngle(0);
			}
			else {
				w.getPlayers()[nbt].setAngle(w.getPlayers()[nbt].getAngle() + mvt);	
			}
		}
		if (msg.charAt(6) ==  ('1')) {

		}
		if (msg == "qu") {
			//Tell the server that one player is off
			w.minusNbPlayersNow();
			//free the thread
			tm.setFree(nbt, true);
			// decrease the number f threads
			tm.minusNbThreads();
			//Kill the thread in a proper way
			tm.getConnexions()[nbt].shinde();
			System.out.println("un thread est mort");
		}
		if (msg == "hello00") {
			System.out.println("hello00");
		}
	}
	
}
